package com.itmk.web.sys_menu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.itmk.annotation.Auth;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.sys_menu.entity.MakeMenuTree;
import com.itmk.web.sys_menu.entity.PermissonVo;
import com.itmk.web.sys_menu.entity.SysMenu;
import com.itmk.web.sys_menu.service.SysMenuService;
import com.itmk.web.sys_user.entity.SysUser;
import com.itmk.web.sys_user.service.SysUserService;
import com.itmk.web.user_menu.entity.AssignParm;
import com.itmk.web.user_menu.service.UserMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 菜单设计
 */
@RestController
@RequestMapping("/api/menu")
public class SysMenuController {
    @Autowired
    UserMenuService userMenuService;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    private SysMenuService sysMenuService;
    //添加
    @Auth
    @CrossOrigin
    @PostMapping()
    public ResultVo add(@RequestBody SysMenu sysMenu){
        sysMenu.setCreateTime(new Date());
        sysMenu.setMenuId(sysMenuService.maxId());
        if (sysMenuService.save(sysMenu)){
            return ResultUtils.success("添加成功");
        }
        return ResultUtils.error("失败");
    }
//编辑
    @Auth
    @CrossOrigin
    @PutMapping()
    public ResultVo edit(@RequestBody SysMenu sysMenu){

        if (sysMenuService.updateById(sysMenu)){
            return ResultUtils.success("编辑成功");
        }
        return ResultUtils.error("失败");
    }
  //删除
    @Auth
    @CrossOrigin
    @DeleteMapping("/{menuId}")
    public ResultVo edit(@PathVariable("menuId") Long menuId){
        if (sysMenuService.removeById(menuId)){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error("失败");
    }

    //列表
    @Auth
    @CrossOrigin
    @GetMapping("/list")
    public ResultVo getList(){
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(SysMenu::getCreateTime);
        List<SysMenu> menuList = sysMenuService.list(queryWrapper);
        //组装数据
        List<SysMenu> list = MakeMenuTree.makeTree(menuList,0L);
        return ResultUtils.success("查询成功",list);
    }
  //查询上级菜单
    @Auth
    @CrossOrigin
    @GetMapping("/getParent")
    public ResultVo getParent(){
        List<SysMenu> list = sysMenuService.getParnet();
        return ResultUtils.success("查询成功",list);
    }

    //分配菜单树数据查询和回显
    @Auth
    @CrossOrigin
    @GetMapping("/getAssignTree")
    public ResultVo getAssignTree(Long userId,Long assId){
//查询当前用户的信息
        SysUser user = sysUserService.getById(userId);
//查询菜单信息
        List<SysMenu> menuList = null;
//判断用户是否是超级管理员，超级管理员拥有所有的权限
        if(StringUtils.isNotEmpty(user.getIsAdmin()) &&
                "1".equals(user.getIsAdmin())){
                    menuList = sysMenuService.list();
        }else{
//根据用户id查询
            menuList = sysMenuService.getMenuByUserId(userId);
        }
//组装树数据
        List<SysMenu> menus = MakeMenuTree.makeTree(menuList, 0L);
//设置菜单数据
        PermissonVo vo = new PermissonVo();
        vo.setMenuList(menus);
//查询回显的数据
        List<SysMenu> menuByUserId = sysMenuService.getMenuByUserId(assId);
        List<Long> ids = new ArrayList<>();
        Optional.ofNullable(menuByUserId).orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(item -> ids.add(item.getMenuId()));
        vo.setCheckList(ids.toArray());
        return ResultUtils.success("查询成功！",vo);
    }
    //保存菜单
    @Auth
    @CrossOrigin
    @PostMapping("/assignSave")
    public ResultVo saveMenu(@RequestBody AssignParm parm){
        SysUser user = sysUserService.getById(parm.getAssId());
        if (StringUtils.isNotEmpty(user.getIsAdmin())&&"1".equals(user.getIsAdmin())){
            return ResultUtils.error("不能为大哥分配管理权限！！！");
        }
        userMenuService.saveParm(parm);
        return ResultUtils.success("分配成功");
    }

}
